/* StorageMonthOutTask.java
 *
 * Project/Product: GW
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   Administrator	2008-5-29	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package system.base;

import java.util.TimerTask;

import framework.core.DBConnection;
import framework.core.DateUtil;


public class StorageMonthOutTask extends TimerTask {
    //private static Log log = LogFactory.getLog(BackUpTableTask.class);

    private static boolean isRunning = false;
    
    private DBConnection con = null;
    public void run() {
        if(!checkDay())
            return;
        if (!isRunning) {
            myRun();
        } else {
            System.out.println("-----------------------------");
        }
    }
    
    //判断是不是可执行的时间
    private boolean checkDay(){
        DateUtil date = new DateUtil();
        int day = date.getDay();
        if(day == 3){
            return true;
        }else{
            return false;
        }
    }
    
    
    private void myRun(){
        try {
            isRunning = true;            
            con = DBConnection.open(false);
            con.setAutoCommit(false);
            String[] time = this.getTime(getYear(), getMonth());
            String balanceTime1 = time[0]+time[1];
            String balanceTime2 = time[0]+"-"+time[1];
            StorageMonthOutSrv.makeStorageMonthOutData(con, balanceTime1,balanceTime2);
            con.commit();
            System.out.println(".............................");
        } catch (Exception e) {
            try {
                con.rollback();
            } catch (Exception e1) {
                
                e1.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            try {
                if (con != null){
                    con.close();
                }
            } catch (Exception e) {
                
                e.printStackTrace();
            }
            isRunning = false;
        }
    }
    
    
    private int getYear(){
        DateUtil date = new DateUtil();
        return date.getYear();
    }
    
    private int getMonth(){
        DateUtil date = new DateUtil();
        return date.getMonth();
    }
    
    private String[] getTime(int year,int month){
        String[] time = new String[2];
        if(month == 1){
            time[1] = "12";
            time[0] = String.valueOf(year - 1);
        }else if(month < 10){
            month = month - 1;
            time[1] = "0"+month;
            time[0] = String.valueOf(year);
        }else{
            time[1] = String.valueOf(month - 1);
            time[0] = String.valueOf(year);
        }
        return time;
    }
}
